<?php

class lb_group{
	
	// Superuser id
	const SU_ID  = 1;
	// Moderator id
	const MDR_ID = 2;
	
	var $errs = array();
	
	/**
	 * Create new group
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param	string	group name
	 * @return	int/bool	group id or false on failure
	 */
	function add_group($group_name){
		global $lb;
		
		if($group_name == ''){
			$this->errs[] = LANG_GROUP_ERROR_NO_NAME;
			return false;
		}
		
		$params = array(
				array($group_name, 'text')
				);
		$query  = 'INSERT INTO #__group (name) VALUES (#)';
		$result = $lb['db']->query($query, $params);
		
		if($result['num_of_rows'] == 1){
			return $result['insert_id'];
		}else{
			$this->errs[] = LANG_GROUP_ERROR_ADD;
			return false;
		}
	}

	/**
	 * Edit group
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param	int		group id
	 * @param	string	group name
	 * @return	int/bool	true or false
	 */
	function edit_group($group_id, $group_name){
		global $lb;
		
		if($group_name == ''){
			$this->errs[] = LANG_GROUP_ERROR_NO_NAME;
			return false;
		}
		
		$params = array(
				array($group_name, 'text'),
				array($group_id,	'int')
				);
		$query  = 'UPDATE #__group SET name = # WHERE group_id = #';
		$result = $lb['db']->query($query, $params);
		
		if($result['num_of_rows'] == 1){
			return true;
		}else{
			$this->errs[] = LANG_GROUP_ERROR_EDIT;
			return false;
		}
	}

	/**
	* Create new group
	*
	* @access	public
	* @author	Armin Borumand
	* @param	int		group id
	* @return	int/bool	true or false
	*/
	function remove_group($group_id){
		global $lb;
		
		if($group_id == self::SU_ID || $group_id == self::MDR_ID){
			$this->errs[] = LANG_GROUP_ERROR_REMOVE_SU;
			return false;
		}
		
		$params = array(
				array($group_id, 'int')
				);
		$query  = 'DELETE FROM #__group WHERE group_id = #';
		$result = $lb['db']->query($query, $params);
		
		if($result['num_of_rows'] == 1){
			return true;
		}else{
			$this->errs[] = LANG_GROUP_ERROR_REMOVE;
			return false;
		}
	}
	/**
	 * list groups
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @return	array		list of group_id=>group_name
	 */
	function list_groups(){
		global $lb;
		$return = array();
		
		$query = 'SELECT * FROM #__group ORDER BY group_id';
		$result = $lb['db']->query($query, array());
		
		if($result['num_of_rows'] > 0){
			foreach($result['rows'] as $rows){
				$return[$rows['group_id']] = $rows['name'];
			}
			return $return;
			
		}else{
			return false;
		}
	}
	
	/**
	 * Return group name from group id
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param		int		group id
	 * @return	string/bool	group name or false
	 */
	function group_name($id){
		global $lb;
		
		$query  = 'SELECT name FROM #__group WHERE group_id = #';
		$params = array(
				array($id, 'int')
				);
		$result = $lb['db']->query($query, $params);
		
		if($result['num_of_rows'] == 1){
			return $result['rows'][0]['name'];
		}else{
			return false;
		}
	}
	
	/**
	 * return group id from group name
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param		string	group name
	 * @return	int/bool	group id or false
	 */
	function group_id($name){
		global $lb;
		
		$query  = 'SELECT group_id FROM #__group WHERE name = #';
		$params = array(
				array($name, 'text')
				);
		$result = $lb['db']->query($query, $params);
		
		if($result['num_of_rows'] == 1){
			return $result['rows'][0]['group_id'];
		}else{
			return false;
		}
	}

	/**
	 * return user group name and id
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @param		integer	user id
	 * @return	array	group id,name
	 */
	function user_group($user_id){
		global $lb;
		
		$query = 'SELECT B.group_id AS id, B.name AS name FROM #__usergroup AS A
				INNER JOIN #__group AS B ON (B.group_id = A.group_id)
				WHERE A.user_id = #';
		$params = array(
					array($user_id, 'int')
					);
		$result = $lb['db']->query($query, $params);
		if($result['result'] && $result['num_of_rows'] > 0){
			return $result['rows'][0];
		}else{
			return false;
		}
	}
	
	/**
	 *return lb_group errors
	 *
	 * @access	public
	 * @author	Armin Borumand
	 * @return	array		lb_group errors
	 */
	function get_errors(){
		$errors = $this->errs;
		$this->errs = array();
		return $errors;
	}
}
?>
